package com.hzlj.position.locate.dao;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.common.base.converter.QueryWrapperConverterPlus;
import com.fz.common.base.converter.RemoveWrapperPlus;
import com.fz.common.base.dto.DeleteCommonDTO;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.hzlj.position.locate.common.dto.reportInspectionAutoSetting.ReportInspectionAutoSettingQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInspectionAutoSetting.ReportInspectionAutoSettingQueryResultDTO;
import com.hzlj.position.locate.common.model.ReportInspectionAutoSetting;
import com.hzlj.position.locate.mapper.ReportInspectionAutoSettingMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 通信联络核查-核查-自动配置表(ReportInspectionAutoSetting)表操作类
 *
 * @author lifh
 * @date 2023-03-13 19:10:14
 */
@Repository
public class ReportInspectionAutoSettingDao extends ServiceImpl<ReportInspectionAutoSettingMapper, ReportInspectionAutoSetting> {
    @Resource
    private ReportInspectionAutoSettingMapper reportInspectionAutoSettingMapper;


    public List<ReportInspectionAutoSetting> getReportInspectionAutoSettingByJgCode(String jgCode) {
        LambdaQueryWrapper<ReportInspectionAutoSetting> queryWrapper = new QueryWrapper<ReportInspectionAutoSetting>().lambda()
                .eq(ReportInspectionAutoSetting::getJgCode, jgCode);
        return this.list(queryWrapper);
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    public boolean updateReportInspectionAutoSetting(ReportInspectionAutoSetting dto) {
        LambdaQueryWrapper<ReportInspectionAutoSetting> queryWrapper = new QueryWrapper<ReportInspectionAutoSetting>().lambda()
                .eq(ReportInspectionAutoSetting::getId, dto.getId());
        return this.update(dto, queryWrapper);
    }

    /**
     * 删除数据
     *
     * @param dto 实例对象
     * @return 是否成功
     */
    public boolean deleteReportInspectionAutoSetting(DeleteCommonDTO<String> dto) {
        return RemoveWrapperPlus.remove(this, dto);
    }


    /**
     * 查询列表：分页
     *
     * @param dto  查询条件
     * @param page 分页信息
     * @return 对象列表
     */
    public QueryPageResultDTO<ReportInspectionAutoSettingQueryResultDTO> pageReportInspectionAutoSetting(ReportInspectionAutoSettingQueryParamDTO dto, PageParamDTO page) {
        return QueryWrapperConverterPlus.of(reportInspectionAutoSettingMapper)
                .page(ReportInspectionAutoSettingQueryResultDTO.class, dto, page);
    }

    public List<ReportInspectionAutoSetting> mostMatchedSetting4Sync(String jgCode, Date date) {
        Date beginOfDay = DateUtil.beginOfDay(date);
        LambdaQueryWrapper<ReportInspectionAutoSetting> queryWrapper = new QueryWrapper<ReportInspectionAutoSetting>().lambda()
                .likeRight(ObjectUtil.isNotEmpty(jgCode), ReportInspectionAutoSetting::getJgCode, jgCode)
                .le(ReportInspectionAutoSetting::getStartDay, beginOfDay)
                .ge(ReportInspectionAutoSetting::getEndDay, beginOfDay)
                .orderByAsc(ReportInspectionAutoSetting::getJgCode)//优先父级
                .orderByDesc(ReportInspectionAutoSetting::getId);//优先后设置的

        return this.list(queryWrapper);
    }

}

